#include "commprd_adapter.hpp"
#include "object_builder.hpp"
#include <string.h>
#include <iterator>

using namespace std;

serialize commprd_adapter::snapshot_buf;

commprd_adapter::commprd_adapter(){
	prod_quit_event = FALSE;
	inited = FALSE;
	connected = FALSE;
	channelnum = 0;
}

commprd_adapter::~commprd_adapter(){
	uninit();
}

BOOL 
commprd_adapter::is_init(){
	return inited;
}

BOOL
commprd_adapter::initialize() {
	inited = TRUE;
	return inited;
}

void
commprd_adapter::uninit() {

	if( !is_init() ){
		return;
	}

	if( is_connect() ){
		disconnect();
	}

	inited = FALSE;
	return;
}

BOOL
commprd_adapter::is_connect() {
	return connected;
}

BOOL 
commprd_adapter::connect() {

	if( !is_init() ){
		initialize();
	}

	if( is_connect() ){
		return TRUE;
	}

	connected = TRUE;
	return connected;
	
}

void 
commprd_adapter::disconnect() {

	if( is_connect() ){
		connected = FALSE;
	}

	return;
}

BOOL
commprd_adapter::is_prod_quit(){
	return prod_quit_event;
}
void
commprd_adapter::quit(){
	prod_quit_event = TRUE;
	return;
}

BOOL
commprd_adapter::configure( STUBPARAM params ){

	prod_id = params.nNodeID;

	safe_strcpy( node_name, params.sNodeName, MAX_NAME_LENGTH );

	device_version = params.nDeviceVersion;

	safe_strcpy( host_ip, params.sConnectIP, MAX_IP_LENGTH );

	host_port = params.nConnectPort;

	safe_strcpy( host_user, params.sConnectUserName, MAX_NAME_LENGTH );

	safe_strcpy( host_passwd, params.sConnectPassword, MAX_NAME_LENGTH );

	SAY( DP_DEBUG, "Param: nNodeID=%d,NodeName=%s,Version=0x%x, IP=%s,Port=%d,User=%s,Password=%s\n",
		params.nNodeID,
		params.sNodeName,
		params.nDeviceVersion,
		params.sConnectIP,
		params.nConnectPort,
		params.sConnectUserName,
		params.sConnectPassword );

	if ( node_name[ 0 ] == '\0' ) {
		SAY( DP_ERROR, "<ERROR_LOG>no param(node_name)\n" );
		return -1;
	}

	if ( host_ip[ 0 ] == '\0' ) {
		SAY( DP_ERROR, "<ERROR_LOG>no param(host_name)\n" );
		return -1;
	}
	if ( host_port <= 0 ) {
		SAY( DP_ERROR, "<ERROR_LOG>no param(host_port)\n" );
		return -1;
	}
	
	return TRUE;
}

u_int32 
commprd_adapter::get_channelnum(){
	return channelnum ;
}

void
commprd_adapter::set_channelnum( u_int32 num ){
	channelnum = num; 
	return ;
}

